package com.itheima.health.service.impl;

import com.itheima.health.BusinessException;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.OrderMapper;
import com.itheima.health.dao.OrderSettingMapper;
import com.itheima.health.dao.SetmealMapper;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@DubboService
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * @Author: 位帅帅
     * @Date: 19:34 2021/1/10
     * @Params: [order]
     * @RerurnType: com.itheima.health.pojo.Order
     * @Description: 添加预约信息
     */
    @Transactional
    @Override
    public Order add(Order order) {
        log.info("[预约-添加]order: {}", order);
        //查询预约设置信息
        OrderSetting temp = new OrderSetting();
        temp.setOrderDate(order.getOrderDate());
        OrderSetting orderSetting = orderSettingMapper.selectOne(temp);
        if (null == orderSetting) {
            throw new BusinessException(MessageConst.SELECTED_DATE_CANNOT_ORDER);
        } else if ((orderSetting.getReservations() == null ? 0 : orderSetting.getReservations()) >= (orderSetting.getNumber() == null ? 0 : orderSetting.getNumber())) {
            throw new BusinessException(MessageConst.ORDER_FULL);
        }
        //查询预约是否重复:同人,同时,同套餐
        Order order_temp = new Order();
        order_temp.setMemberId(order.getMemberId());
        order_temp.setOrderDate(order.getOrderDate());
        order_temp.setSetmealId(order.getSetmealId());
        int count = orderMapper.selectCount(order_temp);
        if (count > 0) {
            throw new BusinessException(MessageConst.HAS_ORDERED);
        }
        //插入数据
        orderMapper.insertSelective(order);
        //修改已预约数
        orderSetting.setReservations((orderSetting.getReservations() == null ? 0 : orderSetting.getReservations()) + 1);
        orderSettingMapper.updateByPrimaryKeySelective(orderSetting);
        order = orderMapper.selectByPrimaryKey(order.getId());
        log.info("[预约-添加]order: {}", order);
        return order;
    }

    /**
     * @Author: 位帅帅
     * @Date: 20:20 2021/1/10
     * @Params: [id]
     * @RerurnType: com.itheima.health.pojo.Order
     * @Description: 根据id查询预约信息
     */
    @Override
    public Order findById(Integer id) {
        log.info("[预约-id查询]id: {}", id);
        Order order = orderMapper.selectByPrimaryKey(id);
        log.info("[预约-id查询]order: {}", order);
        return order;
    }

    /**
     * @Author: 位帅帅
     * @Date: 20:09 2021/1/14
     * @Params: []
     * @RerurnType: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @Description: 统计预约信息
     */
    @Override
    public List<Map<String, Object>> findSetmealCount() {
        List<Map<String, Object>> setmealCount = setmealMapper.findSetmealCount();
        log.info("[套餐-预约统计]setmealCount: {}", setmealCount);
        return setmealCount;
    }
}
